package user;

import book.AnalyzingBorrowedBook;
import book.Book;
import book.BorrowedUidAndBookId;
import book.Library;
import constant.Constant;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 32309
 * Date: 2025-07-17
 * Time: 19:05
 */
public class NormalUser extends User{
    private BorrowedUidAndBookId[] borrowedIDS;
    private static AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();
    private int borrowedCount;
    private Library library = Library.getLibrary();

    public NormalUser(String name, int ID) {
        super(name, ID, "普通用户");
        loadBorrowedBook();
    }

    @Override
    public int display() {
        System.out.println("普通⽤⼾ " + name + " 的操作菜单:");
        System.out.println("0. 切换角色");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有的图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅图书");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个⼈借阅情况");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    private void loadBorrowedBook() {
        BorrowedUidAndBookId[] ids = analyzingBorrowedBook.loadID(Constant.BORROWED_BOOK_FILE);
        borrowedIDS = new BorrowedUidAndBookId[Constant.CAPACITY];
        if (ids == null) {
            borrowedCount = 0;
        }else {
            borrowedCount = ids.length;
            if (borrowedCount > borrowedIDS.length) {
                borrowedIDS = new BorrowedUidAndBookId[borrowedCount];
            }

            // 将借阅信息拷贝到borrowedID中
            for (int i = 0; i < borrowedCount; i++) {
                borrowedIDS[i] = ids[i];
            }
        }
    }
    private void writeBorrowedBook() {
        analyzingBorrowedBook.writeID(borrowedIDS,Constant.BORROWED_BOOK_FILE);
    }
    //借阅图书
    public void borrowBook() {
        library.displayBooks();
        // 没有书不能借阅
        if (library.getBookCount() == 0) {
            System.out.println("对不起暑假没有书籍，你不能借阅！！！");
            return;
        }
        // 输入信息
        System.out.println("请输入你要节约的书籍id：");
        int bookId = scanner.nextInt();
        // 查找书籍
        Book book = library.searchById(bookId);
        if (book == null) {
            System.out.println("不好意思没有你要借阅的该书籍！！！");
            return;
        }
        // 加载借阅书籍
        loadBorrowedBook();
        // 查看借阅情况，观察有没有被借阅过

        for (int i = 0; i < borrowedCount; i++) {
            // 被借阅过了，分两种情况
            if (borrowedIDS[i].getBookId() == book.getBookId()) {
                // 被自己借阅过
                if (ID == borrowedIDS[i].getUserId()) {
                    System.out.println("该书 " + book.getTitle() +" 已经被你节约过！" + "借阅人：" + borrowedIDS[i].getName() + " 你的ID：" + borrowedIDS[i].getUserId());
                    return;
                }else {
                    System.out.println("该书 " + book.getTitle() +" 已经被别人节约过！" + "借阅人：" + borrowedIDS[i].getName() + " 他的ID：" + borrowedIDS[i].getUserId());
                    return;
                }
            }
        }
        // 没有被借阅过，那么你可以借阅
        library.borrowBook(bookId);
        // 将借阅信息存储到数组中
        borrowedIDS[borrowedCount++] = new BorrowedUidAndBookId(ID,bookId,name);
        // 写入到BorrowedUidAndBookId中
        writeBorrowedBook();
        System.out.println("借阅书籍 " + book.getTitle() +" 成功！！！");
    }
    //归还图书
    public void returnBook() {
        // 加载借阅书籍
        loadBorrowedBook();
        // 判断借阅次数
        if (borrowedCount == 0) {
            System.out.println("当前没有任何借阅书籍，你不能归还！！！");
            return;
        }
        // 打印借阅情况
        System.out.println("当前所有的借阅书籍情况如下：");
        library.disBorrowedStatus();
        // 输入归还书籍id
        System.out.println("请输入你要归还的书籍id");
        int bookId = scanner.nextInt();
        // 查找书籍
        Book book = library.searchById(bookId);
        // 为null结束进程
        if (book == null) {
            System.out.println("对不起，并没有该书籍！！！");
            return;
        }
        // 遍历借阅表
        for (int i = 0; i < borrowedCount; i++) {
            // 借阅表中有这本书
            if (borrowedIDS[i].getBookId() == bookId) {
                // 判断是不是自己借阅的这本书
                if (ID == borrowedIDS[i].getUserId()) {
                    // 可以归还
                    library.returnBook(bookId);
                    System.out.println("归还 " + book.getTitle() + " 成功！" + " 归还人：" + borrowedIDS[i].getName());
                    // 将归还的书从借阅表删除
                    borrowedIDS[i] = borrowedIDS[borrowedCount - 1];
                    // 清空最后一个
                    borrowedIDS[borrowedCount - 1] = null;
                    borrowedCount--;
                    // 写入借阅表
                    writeBorrowedBook();
                    return;
                }else {
                    System.out.println("对不起, " + book.getTitle() + " 不是你借阅的，你不能归还！！！" + "借阅人是：" + borrowedIDS[i].getName() + " 他的ID：" + borrowedIDS[i].getUserId());
                    return;
                }
            }
        }
        // 遍历完借阅表，都没有结束进程
        System.out.println("对不起，你没有借阅 " +book.getTitle() + " 不需要归还~~~");
    }
    // 查看个⼈正在借阅情况
    public void viewBorrowBooks() {
        loadBorrowedBook();
        //读取当前借阅所有⽤⼾的借阅书籍的情况
        System.out.println("您的借阅情况如下：");
        if (borrowedCount == 0) {
            System.out.println("对不起，当前借阅表没有任何正在借阅的记录！！！");
            return;
        }
        // 遍历借阅表
        // 设置一个标记
        boolean flag = true;
        for (int i = 0; i < borrowedCount; i++) {
            // 查找与自己借阅id相同的信息
            if (borrowedIDS[i].getUserId() == ID) {
                Book book = library.searchById(borrowedIDS[i].getBookId());
                System.out.println(book);
                flag = false;
            }
        }
        if (flag) {
            System.out.println("对不起，您当前没有正在借阅的书籍！！！");
        }
    }

}
